import {
	apiMixUrlApi
} from "@/api/assemb.js";

function dologin() {
	uni.login({
		provider: 'weixin',
		success: function(code) {
			let param = {
				code: code.code
			};
			uni.showLoading({
				title: '登录中...',
				mask: true
			})
			apiMixUrlApi(param, 1079, "DYLogin").then(data => {
				uni.hideLoading()
				console.log(data, 'data111111');
				if (data.status == 0) {
					// 存储 baseinfopro
					let baseinfopro = uni.getStorageSync('baseinfopro');
					baseinfopro.userid = data.userid;
					baseinfopro.sid = data.sid;
					baseinfopro.openid = data.openid;
					uni.setStorageSync('baseinfopro', baseinfopro);
					vipType(false);
				}
			});
			uni.getUserProfile({
				desc: "需要获取用于完善登录信息",
				success: function(code) {
					uni.setStorageSync('UserProfile', code.userInfo);
				},

				fail(res) {}

			});
		}
	});
	// UserProfile();
}

function UserProfile() {
	uni.showModal({
		title: '温馨提示',
		content: '需要获取您的微信昵称与头像用于完善登录信息，是否同意？',
		success: function(res) {
			if (res.confirm) {
				console.log('用户点击确定');
				uni.getUserProfile({
					desc: "需要获取用于完善登录信息",
					success: function(code) {
						uni.setStorageSync('UserProfile', code.userInfo);
					},

					fail(res) {}

				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}

function calculateVipTime(time) {
	let viptime = time.split(' ')[0];
	let year = viptime.split('-')[0];
	var timestamp = Date.parse(new Date());
	var date = new Date(timestamp);
	var yearNow = date.getFullYear();
	let yearTime = year - yearNow;
	console.log(yearTime, "yearTime");
	if (yearTime >= 30) {
		return '永久会员';
	} else {
		return time.slice(0, 11);
	}
}

function vipType(isJump) {
	apiMixUrlApi({}, 1042, 'GetPaymentInfo').then(data => {
		console.log(data, "data");
		if (data.descinfo == "success") {
			uni.setStorageSync('frequencyrs', data.coins)
			if (data.vipendtime) {
				let viptime = calculateVipTime(data.vipendtime);
				console.log(viptime, "viptime");
				let vipType = {
					vipStatus: data.vipStatus,
					vipendtime: viptime
				};
				uni.setStorageSync('vipType', vipType);
			}
			if (isJump) {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	});
	// let vipType = {
	// 	vipStatus: 1,
	// 	vipendtime: '测试会员'
	// };
	// uni.setStorageSync('vipType', vipType);
}

function isLogin() {
	return !!uni.getStorageSync('baseinfopro').openid ? true : false;
}
/**
 * 判断是否永久会员
 * @param {string} time 会员时间
 * @param {string} type 会员类型（1:vip 2:svip）
 */


export {
	dologin,
	isLogin,
	UserProfile,
	vipType
};